<?php

/**
 * This is the model class for table "product_reservation".
 *
 * The followings are the available columns in table 'product_reservation':
 * @property integer $id
 * @property integer $product
 * @property integer $quantity
 * @property string $date_to_purchase
 * @property string $total_amount
 * @property string $date_time_created
 * @property integer $customer
 *
 * The followings are the available model relations:
 * @property Profiles $customer0
 * @property Product $product0
 */
class ProductReservation extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return ProductReservation the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'product_reservation';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('product, quantity, date_to_purchase', 'required'),
			array('product, quantity', 'numerical', 'integerOnly'=>true),
			array('customer', 'displayUser'),
			array('total_amount', 'displayTotal'),
			array('date_time_created', 'safe'),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('id, product, quantity, date_to_purchase, total_amount, date_time_created, customer', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'customer0' => array(self::BELONGS_TO, 'Profile', 'customer'),
			'product0' => array(self::BELONGS_TO, 'Product', 'product'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'product' => 'Product',
			'quantity' => 'Quantity',
			'date_to_purchase' => 'Date To Purchase',
			'total_amount' => 'Total Amount',
			'date_time_created' => 'Date and Time Created',
			'customer' => 'Customer',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id);
		$criteria->compare('product',$this->product);
		$criteria->compare('quantity',$this->quantity);
		$criteria->compare('date_to_purchase',$this->date_to_purchase,true);
		$criteria->compare('total_amount',$this->total_amount,true);
		$criteria->compare('date_time_created',$this->date_time_created,true);
		$criteria->compare('customer',$this->customer);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}
	
	public function displayTotal( $attribute, $params )
    {
        if($this->product != null){
			$this->total_amount=0;
			$this->total_amount = (($this->quantity) * ($this->product0->unit_price));
        }
		else{
			return $this;
		}
    }
        
    public function displayUser( $attribute, $params )
    {
        $this->customer = Yii::app()->user->id;                
    }
}